<?php

/**
 * 带链接的评论历史数据，关联wiki
 * @auth tangyiwan
 */


ini_set('display_errors', 1);
error_reporting(-1);
ini_set('memory_limit', '512M');
require_once '/data/wwwroot/g_vendor/autoload.php';

use Biz\model\wiki\ObjRelateWiki as mObjRelateWiki;
use Biz\model\wiki\Wiki as mWiki;
use Biz\module\Classes\wiki\RelateWiki as clsRelateWiki;
use Biz\module\Classes\wiki\Wiki as clsWiki;
use Biz\service\wiki\ObjRelateWiki as sObjRelateWiki;
use Doraemon\model\CommentLink;
use Doraemon\model\Store\ShenJianGoods as mShenJianGoods;
use Doraemon\service\Comment as sComment;

$list = [];
$page = 1;
$pageSize = 1000;
do {
    $limit = ($page - 1) * $pageSize;
    var_dump($limit, $pageSize);
    //查询评论是评论主的链接内容
    $startTime = strtotime('2019-06-17');
    $sql = "SELECT * FROM " . CommentLink::getTableName() . " WHERE  `res_id` >0 and `uid` >0 and `uid`=`ouid`  and `status`!=9 and `addtime`>" . $startTime . "  ORDER BY `id` DESC LIMIT " . $limit . " , " . $pageSize;
    $list = CommentLink::query($sql)->fetchAll();
    foreach ($list as $row) {
        var_dump('comment link id:' . $row['id']);
        $commentId = $row['comments_id'];
        $objId = $row['obj_id'];
        $objType = sComment::getCommentRelateWikiType($row["obj_id"]);
        $content = $row['origin_link'];
        $ownerUid = $row['ouid'];
        $uid = $row['uid'];
        $resId = $row['res_id'];
        $info = mShenJianGoods::get(['item_id', 'type'], ['id' => $resId]);
        if ($info && $info['type'] && $info['item_id']) {
            $channel = clsWiki::getChannel($info['type']);
            $wikiInfo = mWiki::getInfoByChannelSpu($channel, $info['item_id']);
            try {
                if ($wikiInfo) {
                    $wikiId = $wikiInfo['id'];
                    var_dump('评论:' . $objId . '关联wiki:' . $wikiId);
                    clsRelateWiki::objRelateWiki($ownerUid, $objId, $objType, $commentId, mObjRelateWiki::REL_OBJ_TYPE_COMMENT, [$wikiId], mObjRelateWiki::TYPE_USER_REL, date('Y-m-d H:i:s', $row['addtime']));
                } else {
                    var_dump('评论:' . $objId . '抓取wiki');
                    sObjRelateWiki::commentRelateWiki($ownerUid, $objId, $objType, $commentId, $content);
                }
            } catch (\Exception $e) {
                var_dump($e->getMessage());
            }
        }
    }
    $page++;
} while ($list);
